package com.org.oracle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
import com.org.oracle.enums.ResponseCodeEnum;
import com.org.oracle.exception.CustomException;
import com.org.oracle.mapper.TopicMapper;
import com.org.oracle.mongodb.entity.Chapter;
import com.org.oracle.mongodb.entity.Circle;
import com.org.oracle.mysql.entity.Topic;
import com.org.oracle.service.TopicService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author heiye
 * @version 1.0
 * @data 2024/8/18 下午4:47
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    private TopicMapper topicMapper;
    @Resource
    private MongoTemplate mongoTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查找全部话题AI标签
     */
    @Override
    public List<Topic> findAll() {
        return topicMapper.selectList(Wrappers.query());
    }

    /**
     * 根据 知识圈id 查找对应的 话题ai
     */
    @Override
    public List<Topic> findByCircleId(String circleId) {
        // 校验参数
        Preconditions.checkArgument(StringUtils.isNotBlank(circleId), ResponseCodeEnum.PARAM_REQUIRE);

        return topicMapper.selectList(Wrappers.<Topic>lambdaQuery().eq(Topic::getCircleId, circleId));
    }

    @Override
    public List<Topic> info() {
        //先找章节
        List<Chapter> chapters = mongoTemplate.findAll(Chapter.class);
        List<Topic> topicList = new ArrayList<Topic>();
        //再根据每一个章节循环的去填充对应的话题ai
        for (Chapter chapter : chapters) {
            String circleId = chapter.getCircleId();
            Query query = new Query();
            query.addCriteria(Criteria.where("circleId").is(circleId));
            Circle circle = mongoTemplate.findOne(query, Circle.class);
            System.out.println(circle);
            chapter.getTesting_points().forEach(testingPoint -> {
                Topic topic = new Topic();
                topic.setCircleId(circleId);
                topic.setTitle(testingPoint.getName());
                topic.setIntroduce(testingPoint.getSummary());
                topic.setAiKey(circle.getAi_key());
                topic.setChapter(testingPoint.getOrdinal());
                topic.setAvatar(circle.getAvatar());
                //保存到ai表里
                //检查circle_id和chapter字段的语句
                QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("circle_id", circleId).eq("chapter", "01");
                if (topicMapper.selectOne(queryWrapper) != null) {
                    topicMapper.updateById(topic);
                } else
                    topicMapper.insert(topic);
                topicList.add(topic);
            });
        }
        return topicList;
    }

    @Override
    public List<Topic> findGoods() {
        //想要得到权重最高的20个话题
        List<Topic> topics = query().orderByDesc("weight").list();
        if (topics.size() > 20) {
            topics = topics.subList(0, 20);
        }
        return topics;

    }

    @Override
    public Object updateWeight(String id, int type) {
        int weight = 0;
        String point = stringRedisTemplate.opsForValue().get("topic_weight_" + id);
        if (point != null)
            weight = Integer.parseInt(point);
        if (String.valueOf(type).equals("1"))
            stringRedisTemplate.opsForValue().set("topic_weight_" + id, weight + 1 + "");
        else
            stringRedisTemplate.opsForValue().set("topic_weight_" + id, weight + 2 + "");
        return null;
    }


}
